package com.chao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chao.entity.Class;
import com.chao.entity.Course;
import com.chao.entity.Schedule;
import com.chao.entity.Teacher;
import com.chao.service.impl.ClassServiceImpl;
import com.chao.service.impl.CourseServiceImpl;
import com.chao.service.impl.ScheduleServiceImpl;
import com.chao.service.impl.TeacherServiceImpl;
import com.chao.utils.TypeConversion;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class CourseController {

    @Autowired
    CourseServiceImpl courseService;
    @Autowired
    ScheduleServiceImpl scheduleService;
    @Autowired
    TeacherServiceImpl teacherService;
    @Autowired
    ClassServiceImpl classService;


    // 修改课程
    @PutMapping("/updateCourse")
    public String course_update(@Valid Course course,
                                @RequestParam Integer pn,
                                RedirectAttributes ra) {
        courseService.updateById(course);
        ra.addAttribute("pn", pn);
        return "redirect:/course_table";
    }


    // 删除课程
    @GetMapping("/deleteCourse/{cIds}")
    public String course_delete(@PathVariable String cIds,
                                @RequestParam Integer pn,
                                RedirectAttributes ra) {
        if (cIds.contains("-")) {
            String[] strings = cIds.split("-");
            List<Integer> integers = TypeConversion.strArrToIntegerList(strings);
            courseService.removeByIds(integers);
        } else {
            courseService.removeById(Integer.parseInt(cIds));
        }
        ra.addAttribute("pn", pn);
        return "redirect:/course_table";
    }

    // 添加课程
    @PostMapping("/course")
    public String course(@Valid Course course,
                         @RequestParam Integer pn,
                         RedirectAttributes ra) {
        System.out.println(pn);
        course.setCDate(new Date());
        courseService.addOfPrimary(course);
        ra.addAttribute("pn", pn);
        return "redirect:/course_table";
    }

    @GetMapping("/course_table")
    public String course_table(@RequestParam(defaultValue = "1") Integer pn,
                               Model model) {
        Page<Course> page = new Page<>(pn, 5);
        Page<Course> coursePage = courseService.page(page);
        List<Course> courses = coursePage.getRecords();
        model.addAttribute("page", page);
        model.addAttribute("courses", courses);
        return "course/course_table";
    }

    /*****
     *
     *
     *
     *
     *
     */
    @GetMapping("/course_arrange_table")
    public String course_arrange(@RequestParam(defaultValue = "1") Integer pn,
                                 Model model) {
        Page<Schedule> page = new Page<>(pn, 5);
        Page<Schedule> schedulePage = scheduleService.selectAll(page);
        List<Schedule> schedules = schedulePage.getRecords();
        model.addAttribute("page", page);
        model.addAttribute("schedules", schedules);
        return "course/course_arrange";
    }

    // 添加
    @PostMapping("/course_arrange")
    public String course_arrange(@Valid Schedule schedule,
                                 @RequestParam Integer pn,
                                 RedirectAttributes ra) {
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("c_name", schedule.getCName());
        Course course = courseService.getOne(courseQueryWrapper);

        QueryWrapper<Class> classQueryWrapper = new QueryWrapper<>();
        classQueryWrapper.eq("c_name", schedule.getClassName());
        Class clazz = classService.getOne(classQueryWrapper);

        schedule.setSCourse(course.getCId());
        schedule.setSClass(clazz.getCId());
        schedule.setSDate(new Date());

        scheduleService.save(schedule);
        ra.addAttribute("pn", pn);
        return "redirect:/course_arrange_table";
    }


    // 删除
    @GetMapping("/deleteSchedule/{sIds}")
    public String schedule_delete(@PathVariable String sIds,
                                  @RequestParam Integer pn,
                                  RedirectAttributes ra) {
        if (sIds.contains("-")) {
            String[] strings = sIds.split("-");
            List<Integer> integers = TypeConversion.strArrToIntegerList(strings);
            scheduleService.removeByIds(integers);
        } else {
            scheduleService.removeById(Integer.parseInt(sIds));
        }

        ra.addAttribute("pn", pn);
        return "redirect:/course_arrange_table";
    }

    // 修改
    @PutMapping("/updateSchedule")
    public String schedule_update(Schedule schedule,
                                  @RequestParam Integer pn,
                                  RedirectAttributes ra) {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("t_name", schedule.getTName());
        Teacher teacher = teacherService.getOne(teacherQueryWrapper);

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("c_name", schedule.getCName());
        Course course = courseService.getOne(courseQueryWrapper);

        QueryWrapper<Class> classQueryWrapper = new QueryWrapper<>();
        classQueryWrapper.eq("c_name", schedule.getClassName());
        Class clazz = classService.getOne(classQueryWrapper);

        schedule.setSTeacher(teacher.getTId());
        schedule.setSCourse(course.getCId());
        schedule.setSClass(clazz.getCId());

        scheduleService.updateById(schedule);
        ra.addAttribute("pn", pn);
        return "redirect:/course_arrange_table";
    }

}
